#include "AllInc.h"

#include "Camera2D.h"

#include "AllDef.h"
#include <Math.h>



Camera2D::Camera2D(): bt(0), scale(3), center(0.5),hand_move(0)
{
}
/*
void Camera2D::MouseMove ( int x, int y )
{
	int dx = x-mx;
	int dy = y-my;
	if(bt)
	{
		if(hand_move)
		{
			center += vec2(-(float)dx,(float)dy)*(2.0f/(scale*w));
		}
		
	}


	mx=x;
	my=y;
}
		
void Camera2D::MouseButton ( int button, int state )
{
	if(state) 
	{
		bt |= (1<<button);
	}
	else
		bt &= ~(1<<button);

	hand_move = (button==0 && state==1);
	
}

void Camera2D::KeyButton ( int key, int state )
{

}

#define SCALE_SPEED 0.8f
void Camera2D::MouseWheel( int val )
{
	float scale1 = (val>0) ? scale*(1.0f/SCALE_SPEED) : scale*SCALE_SPEED;

	float tmpd=1/scale-1/scale1;
	center.x += tmpd* ((2.0f*mx)/w-1);
	center.y += tmpd* (h-2*my)/(float)w;

	scale=scale1;
}*/
void Camera2D::SetCenter(vec2& c)
{
	center=c;
}

void Camera2D::SetupPosition()
{
	glLoadIdentity();
	glScalef(scale,(scale*w)/h,0);
	glTranslatef(-center.x, -center.y, 0);
}

void Camera2D::SetupProjection(int width,int height)
{
	w = width;
	h = height;
	glViewport(0,0,width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
	gluOrtho2D(-1,1,-1,1);

    glMatrixMode(GL_MODELVIEW);
}

vec2 Camera2D::Get2DCoords(ivec2 cursor)
{
	return center + vec2((cursor.x-w/2)/float(w) , (-cursor.y+h/2)/float(w))*2/scale;
}